home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / EDITVIEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.7 KB  |  312 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.8  $
  6. //
  7. // Implementation of class TEditView
  8. //----------------------------------------------------------------------------
  9. #pragma hdrignore SECTION
  10. #include <owl/pch.h>
  11. #if !defined(OWL_EDITVIEW_H)
  12. # include <owl/editview.h>
  13. #endif
  14. #if !defined(OWL_DOCVIEW_RH)
  15. # include <owl/docview.rh>
  16. #endif
  17. #if !defined(OWL_EDITVIEW_RH)
  18. # include <owl/editview.rh>
  19. #endif
  20.  
  21. OWL_DIAGINFO;
  22. DIAG_DECLARE_GROUP(OwlDocView);        // General Doc/View diagnostic group
  23.  
  24. #if !defined(SECTION) || SECTION == 1
  25.  
  26. //
  27. // TEditView response table
  28. //
  29. DEFINE_RESPONSE_TABLE1(TEditView, TEditSearch)
  30.   EV_VN_DOCCLOSED,
  31.   EV_VN_ISWINDOW,
  32.   EV_VN_ISDIRTY,
  33.   EV_VN_COMMIT,
  34.   EV_VN_REVERT,
  35.   EV_WM_NCDESTROY,
  36. END_RESPONSE_TABLE;
  37.  
  38. //
  39. //
  40. //
  41. TEditView::TEditView(TDocument& doc, TWindow* parent)
  42. :
  43.   TEditSearch(parent, GetNextViewId(), (const char*)0),
  44.   TView(doc),
  45.   Origin(0)
  46. {
  47.   Attr.AccelTable = IDA_EDITVIEW;
  48.   if (::FindResource(*GetModule(), TResId(IDM_EDITVIEW), RT_MENU))
  49.     SetViewMenu(new TMenuDescr(IDM_EDITVIEW, 0,2,0,0,0,1, GetModule()));
  50. }
  51.  
  52. //
  53. //
  54. //
  55. void
  56. TEditView::EvNCDestroy()
  57. {
  58. #if !defined(BI_PLAT_WIN32)
  59.   HGLOBAL hdl = (HGLOBAL)::GlobalHandle((uint)GetWindowWord(GWW_HINSTANCE));
  60. #endif
  61.   TEditSearch::EvNCDestroy();// call TWindow::EvNCDestroy, this may be deleted
  62. #if !defined(BI_PLAT_WIN32)
  63.   if (hdl) {
  64.     ::GlobalUnlock(hdl);
  65.     ::GlobalFree(hdl);
  66.   }
  67. #endif
  68. }
  69.  
  70. //
  71. //
  72. //
  73. TEditView::~TEditView()
  74. {
  75. }
  76.  
  77. //
  78. // Does a given HWND belong to this view? Yes if it is us, or a child of us
  79. //
  80. bool
  81. TEditView::VnIsWindow(HWND hWnd)
  82. {
  83.   return hWnd == TWindow::GetHandle() || IsChild(hWnd);
  84. }
  85.  
  86. //
  87. //
  88. //
  89. bool
  90. TEditView::VnDocClosed(int omode)
  91. {
  92.   if (VnIsDirty() || !(omode & ofWrite))  // make sure someone else's write
  93.     return false;
  94.  
  95.   int top = GetFirstVisibleLine();
  96.   uint selbeg;
  97.   uint selend;
  98.   TEdit::GetSelection(selbeg, selend);
  99.   TEdit::Clear();
  100.   LoadData();
  101.   Scroll(0, top);
  102.   TEdit::SetSelection(selbeg, selend);
  103.  
  104.   return true;
  105. }
  106.  
  107. //
  108. //
  109. //
  110. bool
  111. TEditView::LoadData()
  112. {
  113.   istream* inStream;
  114.   if ((inStream = Doc->InStream(ios::in | ios::binary)) == 0) {
  115.     Doc->PostError(IDS_UNABLEOPEN, MB_OK);
  116.     return false;
  117.   }
  118.   inStream->seekg(0L, ios::end);
  119.   unsigned long total = inStream->tellg();
  120.   inStream->seekg(0L, ios::beg);
  121.  
  122.   // Buffer Limit
  123.   //
  124.   uint MaxEditBuf = 32000;
  125. #if defined(BI_PLAT_WIN32)
  126.   if (TSystem::IsNT()) {
  127.     MaxEditBuf = 2000000;
  128.   }
  129. #endif
  130.  
  131.   uint count = (total > MaxEditBuf) ? MaxEditBuf : (uint)total;
  132.   char far* buf = LockBuffer(count + 1);
  133.   if (!buf) {
  134.     delete inStream;
  135. //    TXOutOfMemory::Raise();
  136.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  137.     return false;
  138.   }
  139.  
  140.   uint len;
  141. #if defined(BI_DATA_NEAR)
  142.   char xbuf[512];
  143.   uint pos = 0;
  144.   do {
  145.     len = (count-pos > sizeof(xbuf) ? sizeof(xbuf) : count-pos);
  146.     inStream->read(xbuf, len);
  147.     if (inStream->gcount() != len)
  148.       break;  // if error test again below
  149.     memcpy(buf+pos, (char far*)xbuf, len);
  150.     pos += len;
  151.   } while (pos < count);
  152. #else
  153.   inStream->read(buf, len = count);
  154. #endif
  155.  
  156.   bool status = (inStream->gcount() == len);
  157.   buf[count] = 0;    // 0 terminate buffer
  158.   UnlockBuffer(buf, true);
  159.   delete inStream;   // close file in case process switch
  160.   if (!status)
  161.     Doc->PostError(IDS_READERROR, MB_OK);
  162.  
  163.   return status;
  164. }
  165.  
  166. //
  167. //
  168. //
  169. bool
  170. TEditView::Create()
  171. {
  172.   TRY {
  173.     TEditSearch::Create();   // throws exception TWindow::TXWindow
  174.   }
  175.   CATCH( (TXOwl&) {
  176.     Doc->PostError(IDS_NOMEMORYFORVIEW, MB_OK);
  177.     NotOK();
  178.     return true;   // cannot return false - throws another exception
  179.   })
  180.   if (Doc->GetDocPath() == 0) {
  181.     return true;           // new file, no data to display
  182.   }
  183.   if (!LoadData())
  184.     NotOK();
  185.   return true;
  186. }
  187.  
  188. //
  189. //
  190. //
  191. void
  192. TEditView::PerformCreate(int menuOrId)
  193. {
  194. #if defined(BI_PLAT_WIN32)
  195.   TEdit::PerformCreate(menuOrId);
  196. #else
  197.   HGLOBAL hGlobal = ::GlobalAlloc(GHND, 256);  // will grow as needed
  198.   if (!hGlobal) {
  199.     TXOutOfMemory::Raise();
  200.   }
  201.   uint16 editDS = FP_SEG(::GlobalLock(hGlobal));
  202.   ::LocalInit(editDS, 0, 128);
  203.   ::GlobalUnlock(hGlobal);
  204.   HINSTANCE hInst = HINSTANCE(hGlobal);
  205.  
  206.  
  207.   // Different than TWindows in that we pass our alloc'd DS as the hInst
  208.   //
  209.   TWindow::SetHandle(::CreateWindowEx(Attr.ExStyle,
  210.                            GetClassName(),
  211.                            Title,
  212.                            Attr.Style,
  213.                            Attr.X, Attr.Y, Attr.W, Attr.H,
  214.                            Parent ? Parent->GetHandle() : 0,
  215.                            REINTERPRET_CAST(HMENU,menuOrId),
  216.                            hInst,
  217.                            Attr.Param)
  218.                     );
  219. #endif
  220. }
  221.  
  222. //
  223. //
  224. //
  225. bool
  226. TEditView::VnCommit(bool force)
  227. {
  228.   if (!force && !(VnIsDirty()))
  229.     return true;
  230.  
  231.   ostream* outStream;
  232.   if ((outStream = Doc->OutStream(ios::out | ios::binary)) == 0) {
  233.     Doc->PostError(IDS_UNABLEOPEN, MB_OK);
  234.     return false;
  235.   }
  236.   outStream->seekp(Origin);
  237.  
  238.   bool status = false;
  239.   char far* buf = LockBuffer();
  240.   if (buf) {
  241.     uint count = strlen(buf);
  242. #if defined(BI_DATA_NEAR)
  243.     char xbuf[512];
  244.     uint len;
  245.     uint pos = 0;
  246.     do {
  247.       len = count-pos > sizeof(xbuf) ? sizeof(xbuf) : count-pos;
  248.       memcpy((char far*)xbuf, buf+pos, len);
  249.       outStream->write(xbuf, len);
  250.       if (!outStream->good())
  251.         break;  // if error test again below
  252.       pos += len;
  253.     } while (pos < count);
  254. #else
  255.     outStream->write(buf, count);
  256. #endif
  257.     status = ToBool(outStream->good());
  258.     UnlockBuffer(buf);
  259.     ClearModify();   // reset edit control
  260.   }
  261.   delete outStream;
  262.   if (!status)
  263.     Doc->PostError(IDS_WRITEERROR, MB_OK);
  264.  
  265.   return status;
  266. }
  267.  
  268. //
  269. //
  270. //
  271. bool
  272. TEditView::VnRevert(bool clear)
  273. {
  274.   TEdit::Clear();
  275.   ClearModify();   // reset edit control
  276.   return clear ? true : LoadData();
  277. }
  278.  
  279. #endif
  280. #if !defined(SECTION) || SECTION == 2
  281.  
  282. IMPLEMENT_STREAMABLE2(TEditView, TEditSearch, TView);
  283.  
  284. #if !defined(BI_NO_OBJ_STREAMING)
  285.  
  286. //
  287. //
  288. //
  289. void*
  290. TEditView::Streamer::Read(ipstream& is, uint32 /*version*/) const
  291. {
  292.   ReadBaseObject((TEditSearch*)GetObject(), is);
  293.   ReadBaseObject((TView*)GetObject(), is);
  294.   is >> GetObject()->Origin;
  295.   return GetObject();
  296. }
  297.  
  298. //
  299. //
  300. //
  301. void
  302. TEditView::Streamer::Write(opstream& os) const
  303. {
  304.   WriteBaseObject((TEditSearch*)GetObject(), os);
  305.   WriteBaseObject((TView*)GetObject(), os);
  306.   os << GetObject()->Origin;
  307. }
  308.  
  309. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  310.  
  311. #endif
  312.